﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFCons
{
	public class MessageText : TextBox
	{
		public MessageText()
		{
			ReadOnly = true;
			Multiline = true;
			ScrollBars = ScrollBars.Vertical;
		}

		private int _maxcount = 20;
		public int MaxCount
		{
			get => _maxcount;
			set
			{
				_maxcount = value;
				while (_messages.Count > _maxcount)
				{
					_messages.RemoveAt(0);
				}
			}
		}
		public void ClearMessage()
		{
			_messages.Clear();
			Text = "";
		}

		private List<string> _messages = new List<string>();
		public void AddMessage(string txt)
		{
			lock (this)
			{
				//AppendText(txt);
				_messages.Add(DateTime.Now.ToString("HH:mm:ss fff") + "  " + txt);
				while (_messages.Count > _maxcount)
				{
					_messages.RemoveAt(0);
				}

				string content = "";
				foreach (var msg in _messages)
				{
					content += msg + Environment.NewLine;
				}
				Text = content;

				ScrollToEnd();
			}
		}

		public void ScrollToEnd()
		{
			SelectionStart = Text.Length;
			SelectionLength = 0;
			ScrollToCaret();
		}
	}
}
